﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Odbc;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using MySql.Data.MySqlClient;
using DerptelligenceSite.Models;
using System.Diagnostics;
using DerptelligenceSite.Models.Erik;

namespace DerptelligenceSite.Controllers
{
    public class ErikController : Controller
    {
        //
        // GET: /Erik/
        //private const string ConnStr =
        //"Driver={MySQL ODBC 6.01 Driver};" +
        //"Server=localhost; Port=3306" +
        //"Database=derptelligencenl;uid=root;pwd=tookurjubs;option=3";

        public ActionResult Index()
        {
            ViewData["blogItems"] = MySqlErikDatabase.GetBlogItems();
            ViewData["PageTitle"] = "Blog";
            return View();
        }

        public ActionResult Item(UInt32? id)
        {

            if (!id.HasValue)
            {
                return RedirectToAction("Index");
            }

            IEnumerable<BlogItem> item = MySqlErikDatabase.GetBlogItems(id);
            BlogItem[] items = item.ToArray();
            if (items.Length > 0)
                ViewData["Item"] = item.ToArray()[0];
            ViewData["PageTitle"] = "Item";
            return View();
        }

        public ActionResult SimpleJack()
        {
            ViewData["PageTitle"] = "Simple Jack";
            return View();
        }

        public ActionResult SimpleJackLogs(UInt32? id)
        {
            ViewData["pageTitle"] = "Simple Jack user chat logs";

            IEnumerable<SJChatLogItem> items = MySqlErikDatabase.GetChatLogItems(id);
            if (id == null)
            {
                ViewData["ChatLogs"] = items;
                return View();
            }
            else
            {
                if (items.Count() == 1)
                {
                    ViewData["Item"] = items.First();
                    return View("SimpleJackLogSingle");
                }
                else
                {
                    Debug.Assert(items.Count() == 0);
                    return RedirectToAction("SimpleJackLogs");
                }
            }
        }

        public ActionResult Homework(UInt32? id, UInt32? course)
        {
            ViewData["pageTitle"] = "Homework";
            IEnumerable<HomeworkItem> items = MySqlErikDatabase.GetHomeworkItems(id, course);

            if (id == null)
            {
                ViewData["Courses"] = MySqlErikDatabase.GetCourses();

                ViewData["HomeworkItems"] = items;

                return View("Homework");
            }
            else
            {
                if (items.Count() == 1)
                {
                    ViewData["Item"] = items.First();
                    return View("HomeworkSingle");
                }
                else
                {
                    Debug.Assert(items.Count() == 0);
                    return RedirectToAction("Homework");
                }

            }
        }

        public ActionResult About()
        {
            ViewData["pageTitle"] = "About me";

            return View();
        }
    }
}
